package com.PuzGame.combinenumbers.customViews;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.PuzGame.combinenumbers.R;

public class CustomPopupWindow
{	
	PopupWindow pop;

	Context context;


	LinearLayout llButtonArea, SpecialViews;

	TextView message, title;
	ImageButton btncancel;
	View Positive;
	View Negative;
	View Neutral1;
	View Neutral2;
	View Neutral3;
	View layout;
	LinearLayout.LayoutParams params;


	public CustomPopupWindow(Context c)
	{
		context = c;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layout = inflater.inflate(R.layout.dialog_pop_win,
				(ViewGroup) ((Activity)context).findViewById(R.id.dialogPopWin));
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;

		pop = new PopupWindow(layout, lp.width, lp.height, true);

		message = (TextView) layout.findViewById(R.id.message);
		title = (TextView) layout.findViewById(R.id.title);
		btncancel = (ImageButton) layout.findViewById(R.id.button_close);
		btncancel.setVisibility(View.GONE);

		llButtonArea = (LinearLayout) layout.findViewById(R.id.buttonArea);
		SpecialViews = (LinearLayout) layout.findViewById(R.id.SpecialViews);
		params = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT,      
				LayoutParams.FILL_PARENT
				);
		params.setMargins(3, 0, 3, 0);
		params.gravity = Gravity.CENTER_HORIZONTAL;

	}


	public void Show() {
		pop.showAtLocation(layout, Gravity.CENTER_VERTICAL, 0, 0);

	}	

	public CustomPopupWindow setCancelable( final OnClickListener listener)
	{

		btncancel.setVisibility(View.VISIBLE);
		btncancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pop.dismiss();
				if(listener != null)	
					listener.onClick(v);
			}
		});

		pop.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

		return this;
	}

	public CustomPopupWindow setPositiveButton(boolean dismiss, CharSequence text,int IconResourceId, final OnClickListener listener)
	{
		setButton(Positive, R.drawable.positive_button, dismiss, text, IconResourceId, listener);

		return this;
	}

	public CustomPopupWindow setNegativeButton(boolean dismiss, CharSequence text, int IconResourceId, final OnClickListener listener)
	{
		setButton(Negative, R.drawable.negative_button, dismiss, text, IconResourceId, listener);

		return this;
	}

	public CustomPopupWindow setNeutral1Button(boolean dismiss, CharSequence text, int IconResourceId, final OnClickListener listener)
	{
		setButton(Neutral1, R.drawable.neutral_button, dismiss, text, IconResourceId, listener);

		return this;
	}


	public CustomPopupWindow setNeutral2Button(boolean dismiss, CharSequence text, int IconResourceId, final OnClickListener listener)
	{
		setButton(Neutral2, R.drawable.neutral_button, dismiss, text, IconResourceId, listener);

		return this;
	}

	public CustomPopupWindow setNeutral3Button(boolean dismiss, CharSequence text, int IconResourceId, final OnClickListener listener)
	{
		setButton(Neutral3, R.drawable.neutral_button, dismiss, text, IconResourceId, listener);

		return this;
	}


	private void setButton(View button, int backgroundResourceId, boolean dismiss, CharSequence text, int IconResourceId, final OnClickListener listener)
	{
		boolean OnlyImage = text.equals("") && IconResourceId != 0;

		
		Button temp = new Button(context);
		temp.setLayoutParams(params);
		temp.setBackgroundResource(backgroundResourceId);
		
		if(button == null)
		{
			if(OnlyImage)
			{
				button = new ImageView(context);
				button.setPadding(10, 0, 10, 0);
				
			}
			else
			{
				button = new Button(context);
				((Button)button).setText(text);
			}

			
		}

		button.setLayoutParams(params);
		
		button.setBackgroundResource(backgroundResourceId);
		
		SetOnclick(button, dismiss, listener);

		if(IconResourceId != 0)
		{
			Drawable image = context.getResources().getDrawable(IconResourceId);
			if(OnlyImage)
			{
				((ImageView)button).setImageDrawable(image);	
			}
			else
			{
				((Button)button).setCompoundDrawablesWithIntrinsicBounds(image,null,null,null);
			}
		}

		llButtonArea.addView(button);
	}


	public void SetOnclick(View button, boolean dismiss,
			final OnClickListener listener) {
		if (dismiss)
		{
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					pop.dismiss();
					if(listener != null)
						listener.onClick(v);
				}
			});
		}
		else
		{
			if(listener != null)
				button.setOnClickListener(listener);
		}
	}

	public CustomPopupWindow setMessage(CharSequence text)
	{
		message.setText(text);
		message.setVisibility(View.VISIBLE);

		return this;
	}

	public CustomPopupWindow setTitle(CharSequence text)
	{
		title.setText(text);

		return this;
	}

	public CustomPopupWindow AddSpecialView(View v, LayoutParams ll)
	{
		if(ll == null)
			SpecialViews.addView(v);
		else
			SpecialViews.addView(v, ll);
		return this;
	}













}
